layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 订单包材重量管理
     */
    var OrderWeightBox = {
        tableId: "orderWeightBoxTable"
    };

    /**
     * 初始化表格的列
     */
    OrderWeightBox.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键ID'},
            {field: 'boxCode', sort: true, title: '包材编码'},
            {field: 'weight', sort: true, title: '订单重量(克)'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'updateTime', sort: true, title: '修改时间'},
            {field: 'createUser', sort: true, title: '创建用户'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderWeightBox.search = function () {
        var queryData = {};
        queryData['boxCode'] = $("#boxCode").val();
        table.reload(OrderWeightBox.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    OrderWeightBox.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/orderWeightBox/add';
    };

    /**
     * 导出excel按钮
     */
    OrderWeightBox.exportExcel = function () {
        var checkRows = table.checkStatus(BiCostDiff.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['boxCode'] = $("#boxCode").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/orderWeightBox/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    OrderWeightBox.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/orderWeightBox/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    OrderWeightBox.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/orderWeightBox/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(OrderWeightBox.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderWeightBox.tableId,
        url: Feng.ctxPath + '/orderWeightBox/list',
        page: true,
        limit: 1000,
        limits:[1000,1500,2000,2500],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: OrderWeightBox.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderWeightBox.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OrderWeightBox.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        OrderWeightBox.exportExcel();
    });


    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '订单重量导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/orderWeightBox/toimportExcel',
            end: function () {
                table.reload(OrderWeightBox.tableId);
            }
        });
    });
    // 工具条点击事件
    table.on('tool(' + OrderWeightBox.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            OrderWeightBox.openEditDlg(data);
        } else if (layEvent === 'delete') {
            OrderWeightBox.onDeleteItem(data);
        }
    });
});
